package com.qf.controller;

import com.qf.common.ResponseResult;
import com.qf.entity.Banner;
import com.qf.service.BannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
   @author 86155
   @ClassName: com.qf.controller.BannerController
   @Description: 该类的功能描述
   @date: 2021/9/28 20:00 
*/

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Resource
    private BannerService bannerService;

    //添加banner
    @PostMapping("/add")
    public ResponseResult<Integer> add(Banner banner) {
        try {
            int i = bannerService.addBanner(banner);
            if (i > 0) {
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    //修改
    @PutMapping("/update")
    public ResponseResult<Integer> update(Banner banner) {
        try {
            Integer i = bannerService.updateBanner(banner);
            if (i > 0) {
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
        }
        return ResponseResult.error();
    }

    //删除
    @DeleteMapping("/delete")
    public ResponseResult<Integer> deleteBanner(Long bid) {
        try {
            Integer i = bannerService.deleteBanner(bid);
            if (i > 0) {
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
        }
        return ResponseResult.error();
    }

    //分页查询
    @GetMapping("/select")
    public ResponseResult<List<Banner>> findAllBanner(Long mid, Long uid,
                                                      Integer status, String bannerName,
                                                      @RequestParam(defaultValue = "1") Integer page,
                                                      @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<Banner> banners = bannerService.findAllBanner(mid, uid, status, bannerName, page, size);
            return ResponseResult.success(banners);
        } catch (Exception e) {
            return ResponseResult.error();
        }
    }


}
